package com.site.servlet;

import com.alibaba.fastjson.JSON;
import com.site.service.*;
import com.site.service.impl.*;
import com.site.utils.CommonResult;
import com.site.vo.ReserveTypeAndGoodsVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/viewwarehouse.do")
public class ViewwarehouseServlet extends HttpServlet {
    private ReserveTypeService reserveTypeService = new ReserveTypeServiceImpl();
    private PrintWriter out = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String opr = req.getParameter("opr");
        try {
            out=resp.getWriter();
            switch (opr){
                case "getAll":
                    out.write(JSON.toJSONString(getAll(req,resp)));
                    break;
                case "del":
                    out.write(JSON.toJSONString(del(req,resp)));
                    break;
                case "getByrtidAndrid":
                    out.write(JSON.toJSONString(getByrtidAndrid(req,resp)));
                    break;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }
    private CommonResult getAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        List<ReserveTypeAndGoodsVO> all = new ArrayList<>();
        List<ReserveTypeAndGoodsVO> list = reserveTypeService.allVOCableSize();
        List<ReserveTypeAndGoodsVO> list1 = reserveTypeService.allVOCircuitBoardSize();
        List<ReserveTypeAndGoodsVO> list2 = reserveTypeService.allVOElectricWireSize();
        for (int i = 0; i < list.size(); i++) {
            all.add(list.get(i));
        }
        for (int i = 0; i < list1.size(); i++) {
            all.add(list1.get(i));
        }
        for (int i = 0; i < list2.size(); i++) {
            all.add(list2.get(i));
        }
        return CommonResult.success(all);
    }


    private CommonResult getByrtidAndrid(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String rtid = req.getParameter("rtid");
        String rid = req.getParameter("rid");
        int a = -1;
        int b = -1;
        if (rtid!=""&&rtid!=null) a = Integer.parseInt(rtid);
        if (rid!=""&&rid!=null) b= Integer.parseInt(rid);
        List<ReserveTypeAndGoodsVO> list = reserveTypeService.selectgetByrtidAndrid(a, b);
        if(list.size()>0){
            return CommonResult.success(list);
        }else {
            return CommonResult.success(null);
        }
    }

    private CommonResult del(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        return null;
    }


}